﻿#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <vector>

class TextCounter {
private:
    std::string filename;

    // 统计字符数（包括空格和标点）
    int countCharacters() {
        std::ifstream file(filename);
        if (!file.is_open()) {
            return -1;
        }

        int count = 0;
        char ch;
        while (file.get(ch)) {
            count++;
        }
        file.close();
        return count;
    }

    // 统计单词数（以空格、标点分隔的连续字母序列）
    int countWords() {
        std::ifstream file(filename);
        if (!file.is_open()) {
            return -1;
        }

        int count = 0;
        std::string line;
        bool inWord = false;

        while (std::getline(file, line)) {
            for (char ch : line) {
                if (std::isalpha(ch)) {
                    if (!inWord) {
                        count++;
                        inWord = true;
                    }
                }
                else {
                    inWord = false;
                }
            }
            // 处理行尾情况
            inWord = false;
        }
        file.close();
        return count;
    }

    // 统计句子数（以 . ! ? 作为句子结束标志）
    int countSentences() {
        std::ifstream file(filename);
        if (!file.is_open()) {
            return -1;
        }

        int count = 0;
        std::string line;

        while (std::getline(file, line)) {
            for (char ch : line) {
                if (ch == '.' || ch == '!' || ch == '?') {
                    count++;
                }
            }
        }
        file.close();
        return count;
    }

    // 显示帮助信息
    void showHelp() {
        std::cout << "用法: wc.exe [参数] [文件名]" << std::endl;
        std::cout << "参数:" << std::endl;
        std::cout << "  -c\t统计字符数" << std::endl;
        std::cout << "  -w\t统计单词数" << std::endl;
        std::cout << "  -s\t统计句子数" << std::endl;
        std::cout << "  -a\t统计所有信息" << std::endl;
        std::cout << "示例:" << std::endl;
        std::cout << "  wc.exe -c file.txt" << std::endl;
        std::cout << "  wc.exe -w file.txt" << std::endl;
        std::cout << "  wc.exe -a file.txt" << std::endl;
    }

public:
    TextCounter(const std::string& filename) : filename(filename) {}

    // 处理命令
    void processCommand(const std::string& command) {
        if (command == "-c") {
            int chars = countCharacters();
            if (chars == -1) {
                std::cout << "错误: 无法打开文件 " << filename << std::endl;
            }
            else {
                std::cout << "字符数: " << chars << std::endl;
            }
        }
        else if (command == "-w") {
            int words = countWords();
            if (words == -1) {
                std::cout << "错误: 无法打开文件 " << filename << std::endl;
            }
            else {
                std::cout << "单词数: " << words << std::endl;
            }
        }
        else if (command == "-s") {
            int sentences = countSentences();
            if (sentences == -1) {
                std::cout << "错误: 无法打开文件 " << filename << std::endl;
            }
            else {
                std::cout << "句子数: " << sentences << std::endl;
            }
        }
        else if (command == "-a") {
            int chars = countCharacters();
            int words = countWords();
            int sentences = countSentences();

            if (chars == -1 || words == -1 || sentences == -1) {
                std::cout << "错误: 无法打开文件 " << filename << std::endl;
            }
            else {
                std::cout << "统计结果 (" << filename << "):" << std::endl;
                std::cout << "字符数: " << chars << std::endl;
                std::cout << "单词数: " << words << std::endl;
                std::cout << "句子数: " << sentences << std::endl;
            }
        }
        else {
            std::cout << "错误: 未知参数 '" << command << "'" << std::endl;
            showHelp();
        }
    }
};

int main(int argc, char* argv[]) {
    // 检查参数数量
    if (argc != 3) {
        std::cout << "错误: 参数数量不正确" << std::endl;
        std::cout << "用法: wc.exe [参数] [文件名]" << std::endl;
        std::cout << "使用 wc.exe -h 查看帮助" << std::endl;
        return 1;
    }

    std::string command = argv[1];
    std::string filename = argv[2];

    // 显示帮助
    if (command == "-h" || command == "--help") {
        std::cout << "文本计数统计程序" << std::endl;
        std::cout << "用法: wc.exe [参数] [文件名]" << std::endl;
        std::cout << "参数:" << std::endl;
        std::cout << "  -c\t统计字符数" << std::endl;
        std::cout << "  -w\t统计单词数" << std::endl;
        std::cout << "  -s\t统计句子数" << std::endl;
        std::cout << "  -a\t统计所有信息" << std::endl;
        std::cout << "  -h\t显示帮助信息" << std::endl;
        return 0;
    }

    // 创建计数器并处理命令
    TextCounter counter(filename);
    counter.processCommand(command);

    return 0;
}